package com.simon.jkxj

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView

class MainActivity : AppCompatActivity() {
    private val tag = "MainActivity"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(tag, "onCreate: 创建MainActivity")
        
        // 启用EdgeToEdge
        WindowCompat.setDecorFitsSystemWindows(window, false)
        
        setContentView(R.layout.activity_main)
        
        // 设置状态栏内边距
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        
        // 设置底部导航与导航控制器关联
        try {
            // 找到BottomNavigationView
            val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_navigation_bar)
                ?: run {
                    Log.e(tag, "onCreate: BottomNavigationView未找到")
                    Toast.makeText(this, "底部导航栏未找到", Toast.LENGTH_SHORT).show()
                    return
                }
            
            // 获取NavHostFragment
            val navHostFragment = supportFragmentManager
                .findFragmentById(R.id.nav_host_fragment) as? NavHostFragment
                ?: run {
                    Log.e(tag, "onCreate: NavHostFragment未找到")
                    Toast.makeText(this, "导航主机未找到", Toast.LENGTH_SHORT).show()
                    return
                }
            
            // 获取NavController并设置底部导航
            val navController = navHostFragment.navController
            bottomNavigationView.setupWithNavController(navController)
            
            // 添加导航监听器
            navController.addOnDestinationChangedListener { _, destination, _ ->
                when (destination.id) {
                    R.id.homeFragment -> Log.d(tag, "onDestinationChanged: 导航到首页")
                    R.id.conditioningFragment -> Log.d(tag, "onDestinationChanged: 导航到调理")
                    R.id.diagnosticFragment -> Log.d(tag, "onDestinationChanged: 导航到体质辨别")
                    R.id.myFragment -> Log.d(tag, "onDestinationChanged: 导航到我的")
                    else -> Log.d(tag, "onDestinationChanged: 导航到其他目标: ${destination.id}")
                }
            }
            
            Log.d(tag, "onCreate: 导航设置完成")
        } catch (e: Exception) {
            Log.e(tag, "onCreate: 设置导航时出错", e)
            Toast.makeText(this, "设置导航时出错: ${e.message}", Toast.LENGTH_SHORT).show()
        }
    }
    
    override fun onResume() {
        super.onResume()
        Log.d(tag, "onResume: MainActivity已恢复")
    }
} 